package com.vanysoft.business.example;

import com.vanysoft.framework.utils.JSONResponse;
import com.vanysoft.framework.utils.file.FileException;
import com.vanysoft.framework.utils.file.FilePathUtil;
import com.vanysoft.framework.utils.file.MultipartFileUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/example/upload")
public class ExampleUploadController {

    @RequestMapping
    public String uploadPage() {
        return "example/upload";
    }

    //	文件上传
    @RequestMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8")
    @ResponseBody
//	public String upload(MultipartHttpServletRequest request, UploadDemo demo) {//1
    public String upload(MultipartFile file) {//1
        try {

//				List<MultipartFile> fileList = request.getFiles("file");
//				System.err.println(fileList.size());
//				System.err.println(request.getFileNames().hasNext());
//				System.err.println(demo.getToken());

            String filePath = "D:/BaiduNetdiskDownload/" + file.getOriginalFilename();
            File f = new File(filePath);
            FileUtils.writeByteArrayToFile(f, file.getBytes()); //2
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
            return "wrong";
        }
    }

    // excel文件解析
    @RequestMapping(value = "/uploadFiles", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONResponse uploadFiles(MultipartHttpServletRequest request) throws FileException {


        Map<String, Object> resultMap = new HashMap<>();
        Map<String, MultipartFile> fileMap = request.getFileMap();
        String basePath = FilePathUtil.getBaseFile(request) + "upload" + FilePathUtil.DEFAULT_FILE_SEPARATOR;
        List<String> fileNameList = MultipartFileUtil.saveMultipartFiles(fileMap, basePath + UUID.randomUUID().toString());
        resultMap.put("fileNames", fileNameList);
//			Set<String> fileInputNameSet = new HashSet<>();
//			if (null != fileMap) {
//				fileInputNameSet = fileMap.keySet(); // 文件上传控件中的input标签的name属性值
//				resultMap.put("fileInputNameSet", fileInputNameSet);
//				
//				for (String fileInputName : fileInputNameSet) {
//					MultipartFile multipartFile = fileMap.get(fileInputName);
//					
//					if (multipartFile.isEmpty()) {
//						continue;
//					}
//					
//					MultipartFileUtil.saveFile(multipartFile, "e:/upload/"+multipartFile.getName()+"/"+multipartFile.getOriginalFilename());
//					
////					System.err.println(multipartFile.getName());
////					System.err.println(multipartFile.getOriginalFilename());
////					File f = new File("e:/upload/"+multipartFile.getName()+"/"+multipartFile.getOriginalFilename());
////					FileUtils.writeByteArrayToFile(f, multipartFile.getBytes());
//				}
//			}


        return new JSONResponse(resultMap);
    }
}
